﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:02
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  PropertyRepository : Repository<Property, Property, TbProperty>, IPropertyRepository<Property>
    {
        public override Property CreateEntity()
        {
            return new Property { };
        }
        public override IQueryable<Property> Get()
        {
            return MapQueryable(Entities.TbProperty);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbProperty).Count();
        }
        public override IQueryable<Property> Get(Expression<Func<Property, bool>> expression)
        {
            return MapQueryable(Entities.TbProperty).Where(expression);
        }
        public override IQueryable<Property> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbProperty), sort, order);
        }
        public override IQueryable<Property> GetSort(Expression<Func<Property, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbProperty).Where(expression), sort, order);
        }
        public override void Add(Property t)
        {
            Entities.AddToTbProperty(ReverseMapModel(t));
        }
        public override void Update(Property t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Property t)
        {
            if (Entities.TbProperty.Any(o => o.CoPropertyId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbProperty(ReverseMapModel(t));
            }
        }
        public override void Delete(Property t)
        {
            Entities.TbProperty.Where(o => o.CoPropertyId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Property, bool>> expression)
        {
            MapQueryable(Entities.TbProperty).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Property> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Property>();
        }
      
        public override sealed IQueryable<Property> MapQueryable(IQueryable<TbProperty> queryable)
        {
            return queryable.Select(o => new Property 
            {
         
	           Id=o.CoPropertyId,
	           Key = o.CoKey 
,
	           Value = o.CoValue 
,
	           Type = o.CoType 
,
	           Group = o.CoGroup 
            
            });
        }
      
        public override sealed IQueryable<TbProperty> ReverseMapQueryable(IQueryable<Property> queryable)
        {
            return queryable.Select(o => new TbProperty 
            { 
        
	           CoPropertyId=o.Id,
	           CoKey = o.Key
,
	           CoValue = o.Value
,
	           CoType = o.Type
,
	           CoGroup = o.Group
  
            
            });
        }
      
        public override sealed Property MapModel(TbProperty model)
        {
            return new Property {
	           Id=model.CoPropertyId,
	           Key = model.CoKey
,
	           Value = model.CoValue
,
	           Type = model.CoType
,
	           Group = model.CoGroup
            };
        }
        
        public override sealed TbProperty ReverseMapModel(Property model)
        {
            return new TbProperty { 
	           CoPropertyId=model.Id,
	           CoKey = model.Key
	        
,
	           CoValue = model.Value
	        
,
	           CoType = model.Type
	        
,
	           CoGroup = model.Group
	        
            
            
            };
        }


    }
}



